package com.shujia.flink.window

import org.apache.flink.streaming.api.scala._

object Demo3CountWindow {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    val wordsDS: DataStream[(String, Int)] = linesDS.flatMap(_.split(",")).map((_, 1))

    /**
      * 统计窗口：多少条数据计算一次
      * 滚动窗口
      * 滑动窗口
      *
      */

    wordsDS
      .keyBy(_._1)
      .countWindow(10,5)//每隔5条数据处理最近10条数据
      .sum(1)
      .print()

    env.execute()
  }

}
